package org.br.erp.project.expenseinfo.service.impl;

import org.br.erp.project.expenseinfo.entity.Projectexpenseinfo;
import org.br.erp.project.expenseinfo.entity.Projectexpenseinfoitem;
import org.br.erp.project.expenseinfo.mapper.ProjectexpenseinfoitemMapper;
import org.br.erp.project.expenseinfo.mapper.ProjectexpenseinfoMapper;
import org.br.erp.project.expenseinfo.service.IProjectexpenseinfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 项目费用主表
 * @Author: jeecg-boot
 * @Date:   2024-05-10
 * @Version: V1.0
 */
@Service
public class ProjectexpenseinfoServiceImpl extends ServiceImpl<ProjectexpenseinfoMapper, Projectexpenseinfo> implements IProjectexpenseinfoService {

	@Autowired
	private ProjectexpenseinfoMapper projectexpenseinfoMapper;
	@Autowired
	private ProjectexpenseinfoitemMapper projectexpenseinfoitemMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Projectexpenseinfo projectexpenseinfo, List<Projectexpenseinfoitem> projectexpenseinfoitemList) {
		projectexpenseinfoMapper.insert(projectexpenseinfo);
		if(projectexpenseinfoitemList!=null && projectexpenseinfoitemList.size()>0) {
			for(Projectexpenseinfoitem entity:projectexpenseinfoitemList) {
				//外键设置
				entity.setParentid(projectexpenseinfo.getId());
				projectexpenseinfoitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Projectexpenseinfo projectexpenseinfo,List<Projectexpenseinfoitem> projectexpenseinfoitemList) {
		projectexpenseinfoMapper.updateById(projectexpenseinfo);
		
		//1.先删除子表数据
		projectexpenseinfoitemMapper.deleteByMainId(projectexpenseinfo.getId());
		
		//2.子表数据重新插入
		if(projectexpenseinfoitemList!=null && projectexpenseinfoitemList.size()>0) {
			for(Projectexpenseinfoitem entity:projectexpenseinfoitemList) {
				//外键设置
				entity.setParentid(projectexpenseinfo.getId());
				projectexpenseinfoitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		projectexpenseinfoitemMapper.deleteByMainId(id);
		projectexpenseinfoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			projectexpenseinfoitemMapper.deleteByMainId(id.toString());
			projectexpenseinfoMapper.deleteById(id);
		}
	}
	
}
